package com.xuecheng.learning.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 勾新杰
 * @version 1.0
 * @description: rabbitmq消息配置类
 * @date 2025/3/8 18:20
 */
@Configuration
public class MqMessageConfig {

    /**
     * 序列化为json，不是字节
     * @return MessageConverter
     */
    @Bean
    public MessageConverter messageConverter(ObjectMapper objectMapper){
        // 1.定义消息转换器
        Jackson2JsonMessageConverter jackson2JsonMessageConverter = new Jackson2JsonMessageConverter(objectMapper);
        // 2.配置自动创建消息id，用于识别不同消息，也可以在业务中基于ID判断是否是重复消息
        jackson2JsonMessageConverter.setCreateMessageIds(true);
        return jackson2JsonMessageConverter;
    }
}
